﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.Entities;

namespace LibrarySystem.LibraryUtils.DAL.DAOs
{
    public class Library_Borrowings : Common
    {

        public int GetDataCount(string studentCode = null, int lenderId = 0, int isReturned = -1)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Borrowing> queryBorrow = _context.Library_Borrowings;
            //Filter
            if (!string.IsNullOrEmpty(studentCode))
            {
                queryBorrow = queryBorrow.Where(b => b.Library_Student.Code.Contains(studentCode));
            }
            if (lenderId > 0)
            {
                queryBorrow = queryBorrow.Where(b => b.Library_Employee.Id == lenderId);
            }
            if (isReturned > -1)
            {
                queryBorrow = isReturned == 0 ?
                                            queryBorrow.Where(b => b.IsRetured == false)
                                            : queryBorrow.Where(b => b.IsRetured == true);
            }

            return queryBorrow.Count();
        }

        public List<FrontUserBorrwingModel> GetFrontDataList(int studentId = 0)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Borrowing> queryBorrow = _context.Library_Borrowings;
            if (studentId > 0)
            {
                queryBorrow = queryBorrow.Where(b => b.StudentId == studentId);
            }

            List<Library_Borrowing> borrows =  queryBorrow.ToList();

            List<FrontUserBorrwingModel> borrowList = new List<FrontUserBorrwingModel>();

            foreach (Library_Borrowing br in borrows)
            {
                FrontUserBorrwingModel data = new FrontUserBorrwingModel();
                data.ID = br.Id;
                data.StudentID = br.Library_Student.Code;
                data.LenderID = br.Library_Employee.FirstName + " " + br.Library_Employee.LastName ;
                data.BorrowedDate = (DateTime)br.BorrowedDate;
                data.ReturnedDate = (DateTime)br.ReturnedDate;
                data.IsReturned = (Boolean)br.IsRetured;
                borrowList.Add(data);
            }
            _context.Dispose();
            return borrowList;
        }

        public List<BorrowingModel> GetDataList(int startIndex = 0, int pageSize = 0, string sorting = null,
                    string studentCode = null, int lenderId = 0, int isReturned = -1, int studentID = 0, int borrowingID = 0)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Borrowing> queryBorrow = _context.Library_Borrowings;
            //Filter
            if (studentID > 0)
            {
                queryBorrow = queryBorrow.Where(b => b.LenderId == studentID);
            }
            if (!string.IsNullOrEmpty(studentCode))
            {
                queryBorrow = queryBorrow.Where(b => b.Library_Student.Code.Contains(studentCode));
            }
            if (lenderId > 0)
            {
                queryBorrow = queryBorrow.Where(b => b.Library_Employee.Id == lenderId);
            }
            if (isReturned > -1)
            {
                queryBorrow = isReturned == 0 ?
                                            queryBorrow.Where(b => b.IsRetured == false)
                                            : queryBorrow.Where(b => b.IsRetured == true);
            }
            if (borrowingID > 0)
            {
                queryBorrow = queryBorrow.Where(b => b.Id == borrowingID);
            }
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("StudentID ASC"))
            {
                queryBorrow = queryBorrow.OrderBy(b => b.StudentId);
            }
            else if (sorting.Equals("StudentID DESC"))
            {
                queryBorrow = queryBorrow.OrderByDescending(b => b.StudentId);
            }
            //
            List<Library_Borrowing> borrows = pageSize > 0 ? queryBorrow.Skip(startIndex).Take(pageSize).ToList() : queryBorrow.ToList();

            List<BorrowingModel> borrowList = new List<BorrowingModel>();

            foreach (Library_Borrowing br in borrows)
            {
                BorrowingModel data = new BorrowingModel();
                data.ID = br.Id;
                data.StudentID = br.StudentId;
                data.LenderID = br.LenderId;
                data.BorrowedDate = (DateTime)br.BorrowedDate;
                data.ReturnedDate = (DateTime)br.ReturnedDate;
                data.IsReturned = (Boolean)br.IsRetured;
                data.IsAccepted = (Boolean)br.IsAccepted;
                borrowList.Add(data);
            }
            _context.Dispose();
            return borrowList;
        }

        public BorrowingModel AddNewData(BorrowingModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Borrowing borrow = new Library_Borrowing();
            //Assign parameter
            borrow.StudentId = data.StudentID;
            borrow.LenderId = data.LenderID;
            borrow.BorrowedDate = (DateTime)data.BorrowedDate;
            borrow.ReturnedDate = (DateTime)data.ReturnedDate;
            borrow.IsAccepted = (bool)data.IsAccepted ? true : false;
            borrow.IsRetured = (bool)data.IsReturned ? true : false;
            //Insert into database
            _context.Library_Borrowings.InsertOnSubmit(borrow);
            _context.SubmitChanges();
            //Assign new ID to Model
            data.ID = borrow.Id;
            //
            _context.Dispose();
            return data;
        }

        public bool UpdateData(BorrowingModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Borrowing borrow = _context.Library_Borrowings.Where(b => b.Id == data.ID).FirstOrDefault();
            //Assign Parameter
            borrow.StudentId = data.StudentID;
            borrow.BorrowedDate = (DateTime)data.BorrowedDate;
            borrow.ReturnedDate = (DateTime)data.ReturnedDate;

            
            if (data.IsAccepted && !(Boolean)borrow.IsAccepted)
            {
                borrow.IsAccepted = (bool)data.IsAccepted ? true : false;
                List<Library_BorrowingDetail> details = _context.Library_BorrowingDetails.Where(d => d.BorrowingId == data.ID).ToList();
                Library_Books libBook = new Library_Books();
                //
                foreach (Library_BorrowingDetail d in details)
                {
                    libBook.UpdateQuantity(d.BookId, -d.Quantity);
                }
            }
            if (data.IsReturned && !(Boolean)borrow.IsRetured)
            {
                borrow.IsRetured = (bool)data.IsReturned ? true : false;
                List<Library_BorrowingDetail> details = _context.Library_BorrowingDetails.Where(d => d.BorrowingId == data.ID).ToList();
                Library_Books libBook = new Library_Books();
                //
                foreach (Library_BorrowingDetail d in details)
                {
                    libBook.UpdateQuantity(d.BookId, d.Quantity);
                }
            }     
            //Update into database
            _context.SubmitChanges();
            _context.Dispose();
            return true;
        }

        public bool DeleteData(int Id)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Borrowing borrow = _context.Library_Borrowings.Where(b => b.Id == Id).FirstOrDefault();
            //
            IEnumerable<Library_BorrowingDetail> deletingList = _context.Library_BorrowingDetails.Where(d => d.BorrowingId == borrow.Id);
            _context.Library_BorrowingDetails.DeleteAllOnSubmit(deletingList);
            _context.SubmitChanges();
            //Delete in database
            _context.Library_Borrowings.DeleteOnSubmit(borrow);
            _context.SubmitChanges();
            //
            _context.Dispose();
            return true;
        }
    }
}